﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

namespace Manager
{

    public enum BundleLoadLevel
    {
        height,
        middle,
        low,
    }

    public class AssetBundleLoaderEntity
    {
        //加载文件列表
        private List<string> pathList;
        //加载级别
        public BundleLoadLevel loadLevel;
        //内存
        private Dictionary<string,AssetBundle> bundleDic = new Dictionary<string,AssetBundle>();
        //加载器列表
        private List<AssetBundleLoader> loaderList = new List<AssetBundleLoader>();
        //管理回调
        private Action<AssetBundleLoaderEntity> managerCallBack;
        //外部回调
        private Action interfaceCallBack;
        //进度回调
        private Action<float> progressCallBack;
        //加载完成数
        private uint completeNumber = 0;
        //加载失败数
        private uint errorNumber = 0;

        public AssetBundleLoaderEntity(List<string> _pathList, BundleLoadLevel _loadLevel, Action<AssetBundleLoaderEntity> _managerCallBack,
            Action _interfaceCallBack, Action<float> _progressCallBack)
        {
            pathList = _pathList;
            loadLevel = _loadLevel;
            managerCallBack = _managerCallBack;
            interfaceCallBack = _interfaceCallBack;
            progressCallBack = _progressCallBack;
            completeNumber = 0;
            errorNumber = 0;
        }

        //加载失败
        private void OnLoadError(AssetBundleLoader loader)
        {
            Debug.LogError("加载失败:" + loader.url);
            errorNumber++;
            loaderList.Remove(loader);
            UpdateProgress();
        }

        //加载成功
        private void OnLoadComplete(AssetBundleLoader loader)
        {
            completeNumber++;
            bundleDic[loader.url] = loader.bundle;
            loaderList.Remove(loader);
            UpdateProgress();
        }

        //更新进度
        private void UpdateProgress()
        {
            if (progressCallBack != null)
                progressCallBack((float)completeNumber + (float)errorNumber / (float)pathList.Count);
            if (completeNumber + errorNumber == pathList.Count)
            {
                if (managerCallBack != null)
                    managerCallBack(this);
            }
        }


        ///====================================================外部接口================================================

        //开始加载
        public void StartLoad()
        {
            foreach (string path in pathList)
            {
                AssetBundleLoader loader = new AssetBundleLoader(path, OnLoadComplete, OnLoadError);
                loaderList.Add(loader);
            }
        }

        public void backOut()
        {
            if (interfaceCallBack != null)
                interfaceCallBack();
        }

        public Dictionary<string, AssetBundle> GetAssetBundleDic()
        {
            return bundleDic;
        }

        public void Destory()
        {
            pathList = null;
            bundleDic.Clear();
            bundleDic = null;
            loaderList.Clear();
            loaderList = null;
        }

    }

}
